# coding=utf-8


import os, sys, re, jieba, ljqpy
import jieba.posseg as pseg


def GetText(inpf):
    rpath = os.path.realpath(inpf)
    # oo = os.path.join(os.path.dirname(rpath), 'temp.txt')
    # os.system('python doc2txt.py %s %s' % (rpath, oo))
    # with open(oo) as fin: text = fin.read()
    print(rpath)
    with open(rpath) as fin:
        text = fin.read()
    return text

def GetCompanyName(ss):
    global seg
    seg = [x.word for x in ss]
    ret = set()
    for k, v in enumerate(seg):
        if not v[0] in trie[0]: continue
        z = 0
        for i in range(k, k+20):
            if i >= len(seg): break
            for u in seg[i]:
                if not u in trie[z]:
                    z = -1
                    break
                z = trie[z][u]
            if z == -1: break
            if -1 in trie[z]:
                ret.add(trie[z][-1])
    return ret


def findLegalEntity(seg):
    legalEntities = ljqpy.LoadList('legalEntity.txt')
    for k, v in enumerate(seg[:-1]):
        if v.word in legalEntities:
            idx = k+1
            nextVal = seg[idx]
            val = v.word
            while nextVal.word not in ['\n','。','，','、']:
                if nextVal.word in ['：']:
                    pass
                else:
                    val += nextVal.word
                idx += 1
                nextVal = seg[idx]
            return val
    return None


punctuations = {'|',',','、','。','，',':',';','、','；','：','(',')','（','）','%','\n','\t',' '}
def GetName(seg, companyNames):
    names = ljqpy.LoadList('names.txt')
    firstNames = ljqpy.LoadList('top300firstNames.txt')
    accusers = ljqpy.LoadList('accuser.txt')
    idfs = ljqpy.LoadDict('IDF.txt')

    res = []
    legalEntity = {}
    jiebaNames = []
    lastidx = 0
    lastval = ''
    for k, v in enumerate(seg[:-1]):
        if v.flag == 'nr':
            if len(v.word)>1 and len(v.word)<4 and v.word not in jiebaNames:
                if v.word in idfs and float(idfs[v.word])>14.0:
                    jiebaNames.append(v.word)
        length = len(v.word)

        # if v.word in legalEntities:
        # idx = k+1
        # nextVal = seg[idx]
        # val = v.word
        # while nextVal.word not in ['\n','。','，','、'] and nextVal.flag not in ['p','v','f','vn','uj','d','c','i','b','r','t','m','a']:
        # while nextVal.word not in ['\n','。','，','、']:
        # val += nextVal.word
        # idx += 1
        # nextVal = seg[idx]
        # if nextVal in [' ',' ','：','，']:
        # idx += 1
        # nextVal = seg[idx].word

        # if val not in legalEntity:
        # print(val)
        # legalEntity.append(val)

        if v.word in accusers:
            idx = k+1
            nextVal = seg[idx]
            val = ''
            while nextVal.word not in ['\n','。','，','、']:
                if nextVal.word in ['）','：'] or nextVal.flag in ['x']:
                    pass
                else:
                    val += nextVal.word
                idx += 1
                nextVal = seg[idx]
            if val in accusers:
                pass
            elif val not in legalEntity:
                try:
                    ans = findLegalEntity(seg[lastidx:k])
                except IndexError:
                    ans = None
                if ans is not None:
                    legalEntity[lastval]=ans
                    name = ans[5:]
                    res.append(name)
                lastval = val
                lastidx = k

        if v.word in punctuations:
            pass
        elif length >= 4: # assume Chinese name is less than 4 words.
            continue
        elif length == 1:
            if v.word in punctuations or re.match(r'\d+',v.word) or v.flag != 'n' or v.word not in firstNames:
                continue
            nextVal = seg[k+1]
            if len(nextVal.word)>=3 or nextVal.word in punctuations or re.match(r'\d+',nextVal.word):
                pass
            else:
                name = v.word + nextVal.word
                if name in names:
                    #print(name)
                    res.append(name)

        elif length > 0:
            if v.word[0] in firstNames:
                # if v.word in names and v.flag != 'a':
                if v.word in names:
                    res.append(v.word)
                    # print(v.word, v.flag)

    return set(res), legalEntity

def GenCompTrie():
    global trie
    trie = [{}]
    for uu in ljqpy.LoadList('30wan-company-name.txt'):
        z = 0
        for u in uu:
            if not u in trie[z]:
                trie[z][u] = len(trie)
                trie.append({})
            z = trie[z][u]
        trie[z][-1] = uu
    print(len(trie))
    return trie

def Initialize():
    GenCompTrie()
    jieba.load_userdict("names.txt")
    print('init ok')

def ProcessDocu(inpf, outf=None):
    content = GetText(inpf)
    seg = list(pseg.cut(content))
    cnames = GetCompanyName(seg)
    names, legalEntities = GetName(seg, cnames)
    print(names)
    print(cnames)
    print("公司和法人代表信息：")
    for (k,v) in legalEntities.items():
        print(k+'  '+v)
    print('\n')
    if outf:
        ljqpy.SaveList(names, outf+'_name.txt')
        ljqpy.SaveList(cnames, outf+'_compname.txt')

if __name__ == '__main__':
    Initialize()
    ProcessDocu('testcases/1.txt')
    ProcessDocu('testcases/2.txt')
    ProcessDocu('testcases/3.txt')
    ProcessDocu('testcases/4.txt')
    ProcessDocu('testcases/5.txt')
    ProcessDocu('testcases/6.txt')
    ProcessDocu('testcases/7.txt')
    ProcessDocu('testcases/8.txt')
    ProcessDocu('testcases/9.txt')
    ProcessDocu('testcases/10.txt')
    ProcessDocu('testcases/11.txt')
    ProcessDocu('testcases/12.txt')
    ProcessDocu('testcases/13.txt')
    ProcessDocu('testcases/14.txt')
    ProcessDocu('testcases/15.txt')
    print('completed')
